package com.graduate.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.graduate.model.CountExportToExcel;
import com.graduate.model.FundJournal;
import com.graduate.model.FundJournalExport;
import com.graduate.model.PercentExportToExcel;
import com.graduate.service.IFundJournalService;
import com.graduate.util.ExcelView;
import com.graduate.util.PageInfo;

/**
 * 生成数据报表
 * @author Ann
 *
 */
@Controller
@RequestMapping(value="/dataReport")
public class DataReportController {

	@Autowired
	public IFundJournalService fundJournalService;
	
	/**
	 * 报表统计
	 * @return
	 */
	@RequestMapping(value="/index")
	public String index() {
			return "dataReport/dataStatement";
	}
	
	/**
	 * 数据统计
	 * @return
	 */
	@RequestMapping(value="/dataCount")
	public String dataCount() {
			return "dataReport/dataCount";
	}
	
	
	/**
	 * 数据报表
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/listDataState")
	@ResponseBody
	public PageInfo listDataState(@RequestParam Map<String, Object> map,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows){
		PageInfo pageinfo = new PageInfo(page, rows);
		pageinfo.setCondition(map);
		pageinfo = fundJournalService.listDataState(pageinfo);
		return pageinfo;
		
	}
	
	@RequestMapping(value="/dataExportToExcel")
	@ResponseBody
	public ModelAndView dataExportToExcel(@RequestParam Map<String, Object> condition, ModelMap model) {
		List<FundJournalExport> data = fundJournalService.listDataStateForExcel(condition);
		String filename = "资金流水记录表" + new SimpleDateFormat("yyyyMM").format(new Date()) + ".xls";
		String[] herderProperties = new String[] {"incomeExpense","departmentName", "typeName", "money", "occurtime"};
		String[] herderTitles = new String[] {"收支类型","来源", "账目类型", "流水金额（单位：元）","发生时间"};
		Integer [] widths=new Integer[herderTitles.length];
		for(int i=0;i<herderTitles.length;i++){
			widths[i]=herderTitles[i].length()*1000;
		}
		
		String sheetName="资金流水记录";
		return new ModelAndView(new ExcelView(filename, sheetName, herderProperties, herderTitles, widths, null, data, null), model); 
	}

	
	
	/**
	 * 数据报表
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/listDataCount")
	@ResponseBody
	public PageInfo listDataCount(@RequestParam Map<String, Object> map,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows){
		PageInfo pageinfo = new PageInfo(page, rows);
		pageinfo.setCondition(map);
		pageinfo = fundJournalService.listDataCount(pageinfo);
		return pageinfo;
		
	}
	
	
	@RequestMapping(value="/countExportToExcel")
	@ResponseBody
	public ModelAndView countExportToExcel(@RequestParam Map<String, Object> condition, ModelMap model) {
		List<CountExportToExcel> data = fundJournalService.countExportToExcel(condition);
		String filename = "流水收支统计表" + new SimpleDateFormat("yyyyMM").format(new Date()) + ".xls";
		String[] herderProperties = new String[] {"departmentName", "typeName", "incomeMoney", "expenseMoney"};
		String[] herderTitles = new String[] {"来源", "账目类型", "收入金额（单位：元）","支出金额（单位：元）"};
		Integer [] widths=new Integer[herderTitles.length];
		for(int i=0;i<herderTitles.length;i++){
			widths[i]=herderTitles[i].length()*1000;
		}
		
		String sheetName="流水收支统计";
		return new ModelAndView(new ExcelView(filename, sheetName, herderProperties, herderTitles, widths, null, data, null), model); 
	}

	/**
	 * mainframe导出
	 * @param condition
	 * @param model
	 * @return
	 */
		@RequestMapping(value="/percentExportToExcel")
		@ResponseBody
		public ModelAndView percentExportToExcel(@RequestParam Map<String, Object> condition, ModelMap model) {
			List<PercentExportToExcel> data = fundJournalService.percentExportToExcel(condition);
			String filename = "账目类型收支统计表" + new SimpleDateFormat("yyyyMM").format(new Date()) + ".xls";
			String[] herderProperties = new String[] {"typeName","incomeMoney", "expenseMoney", "amountIncomeMoney", "amountExpenseMoney","typeIncomePct","typeExpensePct"};
			String[] herderTitles = new String[] {"账目类型","账目收入金额（单位：元）","账目支出金额（单位：元）","总收入金额（单位：元）","总支出金额（单位：元）", "账目类型收入占比", "账目类型支出占比"};
			Integer [] widths=new Integer[herderTitles.length];
			for(int i=0;i<herderTitles.length;i++){
				widths[i]=herderTitles[i].length()*1000;
			}
			
			String sheetName="账目类型收支统计";
			return new ModelAndView(new ExcelView(filename, sheetName, herderProperties, herderTitles, widths, null, data, null), model); 
		}

	
	
	
	
   /**
    * dataCount页面，按账目类型
    * @param condition
    * @param model
    * @return
    */
	@RequestMapping(value="/highchartsShow")
	@ResponseBody
	public List<Map<String,Object>> highchartsShow(@RequestParam Map<String, Object> condition, ModelMap model) {
		 List<Map<String,Object>> list=fundJournalService.highchartsShow(condition);
	     return list;
	}
	/**
	 * dataCount页面，按账目来源
	 * @param condition
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/highchartsShow1")
	@ResponseBody
	public List<Map<String,Object>> highchartsShow1(@RequestParam Map<String, Object> condition, ModelMap model) {
		 List<Map<String,Object>> list=fundJournalService.highchartsShow1(condition);
	     return list;
	}
	
	
	/**
	 * mainframe
	 * @param condition
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/highchartsShowWeek")
	@ResponseBody
	public List<Map<String,Object>> highchartsShowWeek(@RequestParam Map<String, Object> condition, ModelMap model) {
		 List<Map<String,Object>> list=fundJournalService.highchartsShowWeek(condition);
	     return list;
	}
	/**
	 * mainframe
	 * @param condition
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/highchartsShowMonth")
	@ResponseBody
	public List<Map<String,Object>> highchartsShowMonth(@RequestParam Map<String, Object> condition, ModelMap model) {
		 List<Map<String,Object>> list=fundJournalService.highchartsShowMonth(condition);
	     return list;
	}
//	
//	@RequestMapping(value="/highchartsShowLastMonth")
//	@ResponseBody
//	public List<Map<String,Object>> highchartsShowLastMonth(@RequestParam Map<String, Object> condition, ModelMap model) {
//		 List<Map<String,Object>> list=fundJournalService.highchartsShowLastMonth(condition);
//	     return list;
//	}
	/**
	 * mainframe页面，本月各账目类型收入支出对比统计表格
	 * @param condition
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/gridShowMonth")
	@ResponseBody
	public List<Map<String,Object>> gridShowMonth(@RequestParam Map<String, Object> condition, ModelMap model) {
		 List<Map<String,Object>> list=fundJournalService.gridShowMonth(condition);
	     return list;
	}
	
}
